<template>
  <div class="box">
    <el-row type="flex" justify="space-between">
      <el-col>
        <i class="el-icon-office-building" v-if="isTop"></i>
        <i class="el-icon-user-solid" v-else></i>
        <span>{{ item.name }}</span>
      </el-col>
      <el-col>
        <div class="box-right">
          <span>{{ item.manager }}</span>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="selete" v-if="!isTop"
                >查看部门</el-dropdown-item
              >
              <el-dropdown-item command="delete" v-if="!isTop"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 导入接口
import { deleteDepartment } from '@/api/departments.js'
export default {
  // item: 保存父组件的信息
  props: ['item', 'isTop'],
  methods: {
    async handleCommand (command) {
      if (command == 'delete') {
        try {
          // 删除部门
          await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示')
          // 调用接口
          await deleteDepartment(this.item.id)
          // 提示
          this.$message.success('删除成功')
          // 重新渲染数据 (子传父，通知父组件更新数据)
          this.$emit('updateData')
        } catch (error) {
          console.log('取消删除')
        }
      }else if(command == 'add'){
        // 将本子组件中保存的父组件的信息传回去
        this.$emit('show', { data: this.item, type: 'add' })
        // console.log(this.item);
      }else{
        this.$emit('show', { data: this.item, type: 'edit' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  i {
    margin-right: 6px;
    vertical-align: middle;
    font-weight: 700;
  }
  span {
    font-weight: 700;
    font-size: 14px;
  }
  .box-right {
    text-align: right;
    span {
      font-weight: normal;
    }
    .el-dropdown {
      margin-left: 30px;
      cursor: pointer;
    }
  }
}
</style>
